package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.QuestionBankRequestDTO;
import com.example.demo.mapper.AttributeManageMapper;
import com.example.demo.mapper.ClothingManageMapper;
import com.example.demo.mapper.QuestionBankManageMapper;
import com.example.demo.mapper.entity.Attribute;
import com.example.demo.mapper.entity.Clothing;
import com.example.demo.mapper.entity.QuestionBank;
import com.example.demo.mapper.entity.Student;
import com.example.demo.service.AttributeManageService;
import com.example.demo.service.QuestionBankManageService;
import com.example.demo.service.QuestionManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionBankManageServiceImpl extends ServiceImpl<QuestionBankManageMapper, QuestionBank> implements QuestionBankManageService {
    @Autowired
    private QuestionBankManageMapper mapper;

    @Override
    public PageVo<QuestionBank> getQuestionBankList(PageBo pageBo) {
        List<QuestionBank> logs = mapper.selectList(new QueryWrapper<>());
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public List<QuestionBank> searchQuestionBankList(QuestionBankRequestDTO request) {
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        if (request.getBankName() != null) {
            queryWrapper.like("bank_name", request.getBankName());
        }
        if (request.getBankId() != 0) {
            queryWrapper.eq("bank_id", request.getBankId());
        }
        List<QuestionBank> result = mapper.selectList(queryWrapper);

        return result;
    }

    @Override
    public Result<String> addQuestionBank(QuestionBank request) {
        if(request!=null){
            if (mapper.insert(request) > 0) {
                return Result.ok("添加成功");
            } else {
                return Result.fail("添加失败");
            }
        }
        return Result.fail("添加失败,填写内容为空");
    }

    @Override
    public Result<String> updateQuestionBank(QuestionBank request) {
     //   QuestionBank nowQuestionBank = mapper.selectById(request.getBankId());
    //    nowQuestionBank.setBankName(request.getBankName());
        if (request.getBankId() != 0) {
            int result = mapper.updateById(request);
            if (result > 0) {
                return Result.ok("修改成功");
            } else {
                return Result.fail("修改失败");
            }
        } else {
            return Result.fail("参数不正确或未提供主键信息");
        }
    }

    @Override
    public Result<String> deleteQuestionBank(QuestionBank request) {
        int count = 0;
        count=mapper.countBankFormTask(request.getBankId());
        if(request!=null && count==0){
            if(mapper.deleteById(request)>0)
                return Result.ok("删除成功");
            else
                return Result.fail("删除失败");
        }
        else if(count>0){
            return Result.fail("删除失败,该题库已被任务使用");
        }
        return Result.fail("删除失败,选择内容为空");
    }

    @Override
    public Result<String> batchDeleteQuestionBank(List<QuestionBank> requestList) {
        //mapper.batchDeleteQuestionBank(requestList);
        if(requestList != null){
            int count = 0;
            for (QuestionBank request : requestList) {
                count += mapper.countBankFormTask(request.getBankId());
            }
            if(count == 0){
                if(mapper.batchDeleteQuestionBank(requestList) > 0) {
                    return Result.ok("批量删除成功");
                } else {
                    return Result.fail("批量删除失败");
                }
            } else if(count > 0){
                return Result.fail("删除失败，选项题库已被任务使用");
            }
            return Result.fail("删除失败");
        }
        return Result.fail("删除失败，选择内容为空");
    }

    @Override
    public List<QuestionBank> getBankList() {
        return mapper.selectList(new QueryWrapper<>());
    }


}
